package com.chenfan.mcn.enums;

import com.chenfan.common.extension.dict.DictTranslator;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;

/**
 * @author tangwei
 * Date:     2021/1/29 10:08
 * Description: 执行单单据状态
 */
@AllArgsConstructor
@SuppressWarnings("AlibabaEnumConstantsMustHaveComment")
public enum ExcutionApprovalStatusEnum implements DictTranslator<Integer, String> {

    UN_EXECUTE(-2, "待发布"),

    EXECUTING(-1, "发布中"),

    EXECUTED(10, "已执行"),

    UN_COMMIT(0, "待提交"),

    APPROVAL(1, "审批中"),

    PASSED(2, "待打款"),

    REJECTED(3, "审批拒绝"),

    CANCEL(4, "已撤回"),

    PAID(5, "已打款"),

    CONFIRMING(6, "确认中"),
    @Deprecated
    CONFIRMED(7, "已确认"),

    CONFIRM_REJECTED(8, "确认驳回"),

    INVALID(9, "已作废");

    @Getter
    private final Integer code;

    @Getter
    private final String desc;

    public Integer code() {
        return this.code;
    }

    public static String getStatusName(Integer code) {
        for (ExcutionApprovalStatusEnum status : ExcutionApprovalStatusEnum.values()) {
            if (status.code.equals(code)) {
                return status.desc;
            }
        }
        return null;
    }

    /**
     * 是否是作废
     * @param approvalStatus
     * @return
     */
    public static boolean isInvalid(Integer approvalStatus) {
        return ExcutionApprovalStatusEnum.INVALID.getCode().equals(approvalStatus);
    }

    /**
     * 是否是审批种
     * @param approvalStatus
     * @return
     */
    public static boolean isApproval(Integer approvalStatus) {
        return ExcutionApprovalStatusEnum.APPROVAL.getCode().equals(approvalStatus);
    }


    /**
     * 是否是确认中
     * @param approvalStatus
     * @return
     */
    public static boolean isConfirming(Integer approvalStatus) {
        return ExcutionApprovalStatusEnum.CONFIRMING.getCode().equals(approvalStatus);
    }

    /**
     * 是否是待打款
     * @param approvalStatus
     * @return
     */
    public static boolean isPassed(Integer approvalStatus) {
        return ExcutionApprovalStatusEnum.PASSED.getCode().equals(approvalStatus);
    }

    /** 根据type任意匹配
     * @param code
     * @param items
     * @return
     */
    public static boolean anyMatch(Integer code, ExcutionApprovalStatusEnum... items) {
        boolean match = false;
        if(code != null) {
            for(ExcutionApprovalStatusEnum item : items) {
                if(item.getCode().intValue() == code.intValue()) {
                    match = true;
                    break;
                }
            }
        }
        return match;
    }

    /**
     * 获取actionName
     * @param code
     * @return
     */
    public static String getActionName(Integer code) {
        if(Objects.isNull(code)) {
            return null;
        }
        switch (code.intValue()) {
            case -1:
                return "开始执行";
            case 1:
                return "提交审批";
            case 4:
                return "审批撤回";
            case 8:
                return "确认驳回";
            case 9:
                return "作废";
            case 10:
                return "确认撤回";
            default:
                return null;
        }
    }

    @Override
    public String translate(Integer code) {
        return ExcutionApprovalStatusEnum.getStatusName(code);
    }
}